object SingleInstance {

}

//class SingleInstance1{
//    private constructor(){
//
//    }
//
//    @Volatile
//    private var mInstance: SingleInstance1? = null
//
//    val Instance: SingleInstance1
//        get() {
//            if (mInstance == null) {
//                synchronized(SingleInstance::class.java) {
//                    if (mInstance == null) {
//                        mInstance = SingleInstance1()
//                    }
//                }
//            }
//            return mInstance!!
//        }
//
//}

class SingleInstance2{
    private constructor(){

    }

    companion object{
        @Volatile
        private var mInstance: SingleInstance2? = null

        fun getInstance():SingleInstance2{
            if (mInstance == null) {
                synchronized(SingleInstance::class.java) {
                    if (mInstance == null) {
                        mInstance = SingleInstance2()
                    }
                }
            }
            return mInstance!!
        }
    }
}

/**
 * 简洁，高效，线程安全，自动支持序列化机制
 * 防止反序列化重新创建新的对象，绝对防止多次实例化
 */
enum class SingleInstance3(){
    INSTANCE;

    fun print(){

    }
}

fun main(){
    SingleInstance3.INSTANCE.print()
    SingleInstance2.getInstance()
}